package com.system.base.pay;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.github.wxpay.sdk.WXPay;
import com.system.anno.PayTemplate;
import com.system.base.ApiResult;
import com.system.config.WXConfig;
import com.system.config.WXPayConfigImpl;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 微信支付
 * 
 * @author kaifa002
 *
 */
@SuppressWarnings("rawtypes")
@Component
@PayTemplate(code = "wx")
@Slf4j
public class WxPayTemplate extends AbstractPayTemplate {

	public WXPay getWxPay() {
		// return new WXPay(new WXPayConfigImpl(), SignType.MD5, true);
		return new WXPay(new WXPayConfigImpl());
	}

	@Override
	public ApiResult pay(Map<String, String> data) {
		log.info("使用wx方式支付");
		WXPay wxPay = getWxPay();
		// Map<String, String> data = wxPay.fillRequestData(data);
		Map<String, String> resMap = new HashMap<>();
		try {
			log.info("微信下单请求报文：{}", JSONUtil.parse(data));
			resMap = wxPay.unifiedOrder(data);
			log.info("微信下单返回报文：{}", JSONUtil.parse(resMap));
			if ("SUCCESS".equalsIgnoreCase(resMap.get("return_code"))) {
				// 获取二维码链接
				String code_url = resMap.get("code_url");
				String prepay_id = resMap.get("prepay_id");
				boolean signVerified = wxPay.isPayResultNotifySignatureValid(resMap);
				if (signVerified) {
					QrConfig qrConfig = new QrConfig(300, 300);
					code_url = QrCodeUtil.generateAsBase64(code_url, qrConfig, "png");
					resMap.put("code_url", code_url);
					return ApiResult.build(200, "下单成功！", resMap);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ApiResult.build(400, "下单失败！", resMap);
	}

	@Override
	public void packageData(Map<String, String> params) {
		log.info("wx-packageData");
		// 设置需要的参数
		params.put("out_trade_no", params.get("no"));
		params.put("device_info", "WEB");
		params.put("body", "weChat莆田");
		params.put("detail", "");
		// params.put("attach", null);
		params.put("total_fee", "1");
		params.put("fee_type", "CNY");
		params.put("trade_type", "NATIVE");
		params.put("product_id", "");
		params.put("openid", "");
		params.put("receipt", "N");
		params.put("notify_url", WXConfig.NOTIFY_URL);
		params.put("spbill_create_ip", "172.0.0.1");
	}

	@Override
	public void signature(Map<String, String> params) throws Exception {
		// WXPay wxpay = getWxPay();
		boolean signVerified = true;
		// boolean signVerified = wxpay.isPayResultNotifySignatureValid(params);
		// 验签失败
		if (!signVerified) {
			params.put("signature", "400");
			return;
		}
		params.put("result_code", "SUCCESS");
		String result_code = params.get("result_code").toString();
		if (!"SUCCESS".equals(result_code.toUpperCase())) {
			params.put("signature", "400");
			return;
		}
		params.put("signature", "200");
	}

	@Override
	public String retSuccess() {
		return notifyToWx("SUCCESS", "OK");
	}

	@Override
	public String retFail() {
		return notifyToWx("FAIL", "FAIL");
	}
	
	public String notifyToWx(String return_code, String return_msg) {
		return "<xml>" + "<return_code><![CDATA[" + return_code + "]]></return_code>" + "<return_msg><![CDATA["
				+ return_msg + "]]></return_msg>" + "</xml> ";
	}

}
